import React, { Component } from "react";
import "./index.css";

export default class Item extends Component {
  state = { mouse: false };
  // 删除 按钮
  remove = (id) => {
    const { removeItem } = this.props;
    return () => {
      if (window.confirm("确定删除吗?")) {
        removeItem(id);
      }
    };
  };

  changeCheck = (id) => {
    const { changeChecked } = this.props;
    return (event) => {
      const { checked } = event.target;
      changeChecked(id, checked);
    };
  };
  mouseChange = (target) => {
    return (event) => {
      this.setState({ mouse: target });
    };
  };
  render() {
    const { value, name, id } = this.props;
    const { mouse } = this.state;
    return (
      <li
        onMouseEnter={this.mouseChange(true)}
        onMouseLeave={this.mouseChange(false)}
        style={{ backgroundColor: mouse ? "#ddd" : "#fff" }}
      >
        <label>
          <input
            onChange={this.changeCheck(id)}
            checked={value}
            type="checkbox"
          />
          <span>{name}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? "block" : "none" }}
          onClick={this.remove(id)}
        >
          删除
        </button>
      </li>
    );
  }
}
